package com.sdry.system.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sdry.system.entity.RoleUser;
import com.sdry.system.entity.SysAppMenu;
import com.sdry.system.mapper.AppMenuMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

/**
 * @Description app菜单
 * @Author chenBin
 * @Date 2020-04-07 13:57
 * @Version 1.0
 **/
@Service
@Transactional(rollbackFor = Exception.class)
public class AppMenuServiceImpl implements AppMenuService{

    @Resource
    private AppMenuMapper appMenuMapper;

    @Resource
    private UserService userService;

    @Resource
    private RoleService roleService;

    @Override
    public List<SysAppMenu> queryAppMenuList(String userId) {
        //当前用户所有的角色
        List<RoleUser> roleUsers =  userService.queryRoleUserByUserIdList(Long.parseLong(userId));
        //当前角色对应的菜单
        List<Long> menuIdList = new ArrayList<>();
        for (RoleUser roleUser : roleUsers) {
            List<Long> list = roleService.queryAppRoleMenuList(roleUser.getRoleId());
            menuIdList.addAll(list);
        }
        HashSet set = new HashSet(menuIdList);
        menuIdList.clear();
        menuIdList.addAll(set);
        //最新菜单
        List<SysAppMenu> newSysAppMenuList2 = new ArrayList<>();
        //父菜单
        QueryWrapper<SysAppMenu> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("p_id",0L).eq("del_flag",0L).orderByAsc("seq");
        List<SysAppMenu> sysAppMenuList = appMenuMapper.selectList(queryWrapper);
        //子菜单
        for (SysAppMenu sysAppMenu : sysAppMenuList) {
            QueryWrapper<SysAppMenu> queryWrapper02 = new QueryWrapper<>();
            queryWrapper02.eq("p_id",sysAppMenu.getAmId()).eq("del_flag",0L).orderByAsc("seq");
            List<SysAppMenu> sysAppMenuList2 = appMenuMapper.selectList(queryWrapper02);

            int count = 0;

            //判断是否在用户的菜单里
            for (SysAppMenu appMenu : sysAppMenuList2) {
                if (menuIdList.contains(appMenu.getAmId())) {
                    //newSysAppMenuList2.add(appMenu);
                    count ++;

                }
            }
            if (count > 0) {
                //父菜单放入新菜单
                newSysAppMenuList2.add(sysAppMenu);
            }
        }
        return newSysAppMenuList2;
    }

    @Override
    public List<SysAppMenu> queryAppMenuWenList(Long roleId) {
        //最新菜单
        List<SysAppMenu> newSysAppMenuList2 = new ArrayList<>();
        //父菜单
        QueryWrapper<SysAppMenu> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("p_id",0L).eq("del_flag",0L).orderByAsc("seq");
        List<SysAppMenu> sysAppMenuList = appMenuMapper.selectList(queryWrapper);
        //子菜单
        for (SysAppMenu sysAppMenu : sysAppMenuList) {
            newSysAppMenuList2.add(sysAppMenu);
            QueryWrapper<SysAppMenu> queryWrapper02 = new QueryWrapper<>();
            queryWrapper02.eq("p_id",sysAppMenu.getAmId()).eq("del_flag",0L).orderByAsc("seq");
            List<SysAppMenu> sysAppMenuList2 = appMenuMapper.selectList(queryWrapper02);
            newSysAppMenuList2.addAll(sysAppMenuList2);
        }
        return newSysAppMenuList2;
    }

    @Override
    public List<SysAppMenu> queryAppMenuSonList(String amId, String userId) {
        //当前用户所有的角色
        List<RoleUser> roleUsers =  userService.queryRoleUserByUserIdList(Long.parseLong(userId));
        //当前角色对应的菜单
        List<Long> menuIdList = new ArrayList<>();
        for (RoleUser roleUser : roleUsers) {
            List<Long> list = roleService.queryAppRoleMenuList(roleUser.getRoleId());
            menuIdList.addAll(list);
        }
        HashSet set = new HashSet(menuIdList);
        menuIdList.clear();
        menuIdList.addAll(set);
        //最新菜单
        List<SysAppMenu> newSysAppMenuList2 = new ArrayList<>();
        //父菜单
        QueryWrapper<SysAppMenu> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("p_id",0L).eq("del_flag",0L).orderByAsc("seq");
        List<SysAppMenu> sysAppMenuList = appMenuMapper.selectList(queryWrapper);

        //子菜单
        for (SysAppMenu sysAppMenu : sysAppMenuList) {
            QueryWrapper<SysAppMenu> queryWrapper02 = new QueryWrapper<>();
            queryWrapper02.eq("p_id",sysAppMenu.getAmId()).eq("del_flag",0L).orderByAsc("seq");
            List<SysAppMenu> sysAppMenuList2 = appMenuMapper.selectList(queryWrapper02);
            //父菜单放入新菜单
            newSysAppMenuList2.add(sysAppMenu);
            //判断是否在用户的菜单里
            for (SysAppMenu appMenu : sysAppMenuList2) {
                if (menuIdList.contains(appMenu.getAmId())) {
                    newSysAppMenuList2.add(appMenu);
                }
            }
        }
        List<SysAppMenu> newSysAppMenuSonList = new ArrayList<>();
        for (SysAppMenu sysAppMenu : newSysAppMenuList2) {
            System.out.println(sysAppMenu.getpId());
            if (amId.equals(String.valueOf(sysAppMenu.getpId()))){
                newSysAppMenuSonList.add(sysAppMenu);
            }
        }
        return newSysAppMenuSonList;
    }
}
